<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="优惠券名称" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入优惠券名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item   label="门店名称" prop="storeId">
        <treeselect
          :defaultExpandLevel=1
          v-model="queryParams.storeId"
          :options="storeOptions"
          :normalizer="normalizer"
          placeholder="请输入或选择门店名称"
          style="width: 350px"/>
      </el-form-item>
      <el-form-item label="归属 平台/门店" prop="belongType">
        <el-select v-model="queryParams.belongType" placeholder="请选择归属 平台/门店" clearable size="small">
          <el-option
            v-for="dict in belongTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否新人券" prop="newUserState">
        <el-select v-model="queryParams.newUserState" placeholder="请选择是否新人券" clearable size="small">
          <el-option
            v-for="dict in newUserStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠券类型" prop="couponType">
        <el-select v-model="queryParams.couponType" placeholder="请选择优惠券类型" clearable size="small">
          <el-option
            v-for="dict in couponTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用时间" prop="useTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.useTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择使用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="领取 /赠送" prop="receiveType">
        <el-select v-model="queryParams.receiveType" placeholder="请选择领取 /赠送" clearable size="small">
          <el-option
            v-for="dict in receiveTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :height="tableHeight" :data="couponsMemberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="优惠券名称" align="center" prop="couponName" />
      <el-table-column label="优惠券金额" align="center" prop="money" />
      <el-table-column label="满多少元可用" align="center" prop="minPrice" />
      <el-table-column label="归属 平台/门店" align="center" prop="belongType">
        <template slot-scope="scope">
          <dict-tag :options="belongTypeOptions" :value="scope.row.belongType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否新人券" align="center" prop="newUserState">
        <template slot-scope="scope">
          <dict-tag :options="newUserStateOptions" :value="scope.row.newUserState"/>
        </template>
      </el-table-column>
      <el-table-column label="优惠券类型" align="center" prop="couponType">
        <template slot-scope="scope">
          <dict-tag :options="couponTypeOptions" :value="scope.row.couponType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否已使用" align="center" prop="useState" />
      <el-table-column label="使用时间" align="center" prop="useTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="可使用开始时间" align="center" prop="useStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="可使用结束时间" align="center" prop="useEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取 /赠送" align="center" prop="receiveType">
        <template slot-scope="scope">
          <dict-tag :options="receiveTypeOptions" :value="scope.row.receiveType"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listCouponsMember, getCouponsMember, logicDelCouponsMember, addCouponsMember, updateCouponsMember, exportCouponsMember } from "@/api/coupons/couponsMember";
import { storeListParam } from "@/api/store/store";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "CouponsMember",
  components:{Treeselect},
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 优惠券领取表格数据
      couponsMemberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 归属 平台/门店字典
      belongTypeOptions: [],
      // 是否新人券字典
      newUserStateOptions: [],
      // 优惠券类型字典
      couponTypeOptions: [],
      // 领取 /赠送字典
      receiveTypeOptions: [],
      storeOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponId: null,
        memberId: null,
        couponName: null,
        money: null,
        minPrice: null,
        belongType: null,
        storeId: null,
        newUserState: null,
        couponType: null,
        primaryKey: null,
        useState: null,
        useTime: null,
        useStartTime: null,
        useEndTime: null,
        receiveType: null,
        giveUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.calHeight()
      this.getDicts("jy_belong_type").then(response => {
      this.belongTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.newUserStateOptions = response.data;
    });
    this.getDicts("jy_coupon_type").then(response => {
      this.couponTypeOptions = response.data;
    });
    this.getDicts("receive_type").then(response => {
      this.receiveTypeOptions = response.data;
    });
    storeListParam().then(res =>{
      this.storeOptions = res
    })
  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    normalizer(node) {
      return {
        id: node.dictValue,
        label: node.dictLabel,
      };
    },
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询优惠券领取列表 */
    getList() {
      this.loading = true;
      listCouponsMember(this.queryParams).then(response => {
        this.couponsMemberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        couponId: null,
        memberId: null,
        couponName: null,
        money: null,
        minPrice: null,
        belongType: null,
        storeId: null,
        newUserState: null,
        couponType: null,
        primaryKey: null,
        useState: null,
        useTime: null,
        useStartTime: null,
        useEndTime: null,
        receiveType: null,
        giveUserId: null,
        delState: null,
        delUser: null,
        delType: null,
        delTime: null,
        createBy: null,
        createType: null,
        createTime: null,
        updateBy: null,
        updateType: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券领取";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCouponsMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券领取";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCouponsMember(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCouponsMember(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelCouponsMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有优惠券领取数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCouponsMember(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
